package top.wherewego.vnt.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class VntEditView extends androidx.appcompat.widget.AppCompatEditText {
    public VntEditView(@NonNull Context context) {
        super(context);
        init();
    }

    public VntEditView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public VntEditView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        this.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(" ")) {
                    String filteredText = s.toString().replace(" ", "");
                    VntEditView.this.setText(filteredText);
                    VntEditView.this.setSelection(filteredText.length());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}
